package myJs.user

import myJs.Implicits._
import myJs.Utils._
import myJs.components.ExportDatComponent
import myJs.myPkg._
import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.myPkg.jquery._
import myJs.tool.Tool
import myJs.tool.Tool.{layerOptions, zhInfo}
import org.scalajs.dom._
import org.scalajs.dom.ext.Ajax
import scalatags.Text.all._
import scalatags.Text.{TypedTag, all}
import shared.VarTool

import scala.collection.immutable.SeqMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.{Date, JSON}
import myJs.Implicits._

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("DatExportModal")
object DatExportModal {

  val modalId="exportDatModal"
  val formId="form"

  @JSExport("init")
  def init = {
    bootStrapValidator
    val state=ExportDatComponent.State.init.copy(formJq = $(s"#${modalId} #${formId}"))
    val component = ExportDatComponent.component(state)().renderIntoDOM(document.querySelector(s"#${modalId} #myDat"))
    registerEvent
  }

  def registerEvent={
    $(document.body).on("click", ".myExportDat", "", (y: Element) => {
      exportDat
    })
  }

  def exportDat = {
    val bv = jQuery(s"#${modalId} #${formId}").data("bootstrapValidator")
    bv.validate()
    val valid = bv.isValid().toB
    if (valid) {
      val ids = $("label.mySamples").mapElems(y => $(y).text.trim)
      val exportDatMap = Tool.getExportDatMap(modalId)
      val data = (Map("ids" -> ids.toJSArray) ++ exportDatMap).toJSDictionary
      val url = g.jsRoutes.controllers.SampleController.exportDta().url.toString
      Ajax.post(url = s"${
        url
      }", data = JSON.stringify(data),
        headers = Map("Content-Type" -> "application/json"), responseType = "blob").map {
        xhr =>
          jQuery(s"#${modalId}").modal("hide")
          val fileName = if (ids.size == 1) s"${
            ids.head
          }_" else ""
          if (exportDatMap("hasGroup").toB) {
            Tool.downloadZip(s"${fileName}${exportDatMap("kitName")}.zip", xhr.response)
          } else {
            Tool.downloadTxt(s"${fileName}${exportDatMap("kitName")}.dat", xhr.response)
          }

      }
    }
  }

  def bootStrapValidator = {
    val maxNumber = Double.MaxValue
    val dict = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "glyphicon glyphicon-remove",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "sampleNum" -> {
          val info = "单文件样本数"
          js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> s"${info}不能为空!"
            ),
            "validators" -> js.Dictionary(
              "integer" -> js.Dictionary(
                "message" -> s"${info}必须为整数",
              ),
              "between" -> js.Dictionary(
                "min" -> 1,
                "max" -> maxNumber,
                "message" -> s"${info}必须大于0",
              ),
            )
          )
        },
      )
    )
    g.$(s"#${modalId} #${formId}").bootstrapValidator(dict)
  }





}
